//
//  NYAllViewCell.m
//  百思不得姐
//
//  Created by Apple on 15/8/23.
//  Copyright (c) 2015年 Neal. All rights reserved.
//

#import "NYAllViewCell.h"
#import "NYTopicModel.h"
#import "NYTopConment.h"
#import "NYTopUser.h"

#import "NYTopicPictureView.h"
#import "NYTopicVideoView.h"
#import "NYTopicVoiceView.h"

@interface NYAllViewCell ()

@property (weak, nonatomic) IBOutlet UIImageView *profileImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *createTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *text_label;
@property (weak, nonatomic) IBOutlet UIButton *dingButton;
@property (weak, nonatomic) IBOutlet UIButton *caiButton;
@property (weak, nonatomic) IBOutlet UIButton *shareButton;
@property (weak, nonatomic) IBOutlet UIButton *conmentButton;
@property (weak, nonatomic) IBOutlet UILabel *conmentLabel;
@property (weak, nonatomic) IBOutlet UIView *topconmentView;
/* 图片View */
@property(nonatomic , weak) NYTopicPictureView *contentPictureView;
/* 声音View */
@property(nonatomic , weak) NYTopicVoiceView *contentVoiceView;
/* 视频View */
@property(nonatomic , weak) NYTopicVideoView *contentVideoView;


@end
@implementation NYAllViewCell

- (NYTopicPictureView *)contentPictureView
{
    if (_contentPictureView == nil) {
        NYTopicPictureView *contentPictureView = [NYTopicPictureView viewFromXib];
        [self.contentView addSubview:contentPictureView];
        _contentPictureView = contentPictureView;
    }
    return _contentPictureView;
}
- (NYTopicVoiceView *)contentVoiceView
{
    if (_contentVoiceView == nil) {
        NYTopicVoiceView *contentVoiceView = [NYTopicVoiceView viewFromXib];
        [self.contentView addSubview:contentVoiceView];
        _contentVoiceView = contentVoiceView;
    }
    return _contentVoiceView;
}
- (NYTopicVideoView *)contentVideoView
{
    if (_contentVideoView == nil) {
        NYTopicVideoView *contentVideoView = [NYTopicVideoView viewFromXib];
        [self.contentView addSubview:contentVideoView];
        _contentVideoView  = contentVideoView;
    }
    return _contentVideoView;
}

#pragma mark - 初始化
- (void)awakeFromNib
{
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainCellBackground"]];
}


- (IBAction)shareSheet:(id)sender {
    /**
     iOS8之后的UIAlertController，进行提醒
     */
    UIAlertController *alertvc = [[UIAlertController alloc]init];
    [alertvc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }]];
    [alertvc addAction:[UIAlertAction actionWithTitle:@"收藏" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
    }]];
    [alertvc addAction:[UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
    }]];

    [self.window.rootViewController presentViewController:alertvc animated:YES completion:^{
        
    }];
    
}

- (void)setTopic:(NYTopicModel *)topic
{
    _topic = topic;

    [self.profileImageView setHeaderImageWithUrl:topic.profile_image];
    self.nameLabel.text = topic.name;
    /**
     *  优化显示时间
     */
    self.createTimeLabel.text = topic.create_time;

    /**
     *  文本
     */
    self.text_label.text = topic.text;
    /**
     *  中间内容
     */
    switch (topic.cellType) {
        case NYTopicCellTypePicture:
            self.contentVideoView.hidden = YES;
            self.contentVoiceView.hidden = YES;
            self.contentPictureView.hidden = NO;
            self.contentPictureView.topic = topic;
            self.contentPictureView.frame = topic.contentFrame;
            break;
        case NYTopicCellTypeVoice:
            self.contentVideoView.hidden = YES;
            self.contentVoiceView.hidden = NO;
            self.contentPictureView.hidden = YES;
            self.contentVoiceView.topic = topic;
            self.contentVoiceView.frame = topic.contentFrame;

            break;
        case NYTopicCellTypeVideo:
            self.contentVideoView.hidden = NO;
            self.contentVoiceView.hidden = YES;
            self.contentPictureView.hidden = YES;
            self.contentVideoView.topic = topic;
            self.contentVideoView.frame = topic.contentFrame;
            break;

        default:NYTopicCellTypeText:
            self.contentVideoView.hidden = YES;
            self.contentVoiceView.hidden = YES;
            self.contentPictureView.hidden = YES;
            break;
    }
    /**
     *  热门评论
     */
    if (topic.top_cmt) {
        self.topconmentView.hidden = NO;
        self.conmentLabel.text = [NSString stringWithFormat:@"%@：%@",topic.top_cmt.user.username,topic.top_cmt.content];
    } else {
        self.topconmentView.hidden = YES;
    }
    /**
     *  底部条
     */
    [self setupToolBarWithButton:self.dingButton count:topic.ding title:@"赞"];
    [self setupToolBarWithButton:self.caiButton count:topic.cai title:@"踩"];
    [self setupToolBarWithButton:self.shareButton count:topic.repost title:@"分享"];
    [self setupToolBarWithButton:self.conmentButton count:topic.comment title:@"讨论"];

}
- (void)setupToolBarWithButton:(UIButton *)button count:(NSInteger)count title:(NSString *)title
{
    if (count >= 10000) {
        
        [button setTitle:[NSString stringWithFormat:@"%.1f万",count / 10000.0] forState:UIControlStateNormal];
        
    } else if (count > 0) {
        [button setTitle:[NSString stringWithFormat:@"%ld",count] forState:UIControlStateNormal];
    }else
    {
        [button setTitle:title forState:UIControlStateNormal];
    }
}
/**
 *  设置cell之间的间隙
 *
 */
- (void)setFrame:(CGRect)frame
{
    frame.size.height -= Margin;
    [super setFrame:frame];
}
@end
